import 'package:flutter/material.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/http/api.dart';
import 'package:flutter2/http/dio_manager.dart';
import 'package:flutter2/model/person_info_entity.dart';
import 'package:flutter2/ui/add_card.dart';
import 'package:flutter2/widget/base/base_container.dart';
import 'package:flutter2/widget/base/normal_button.dart';
import 'package:flutter2/widget/info_bar.dart';
import 'package:flutter2/widget/step_view.dart';
import 'package:flutter2/widget/base/normal_text.dart';
import 'package:flutter2/widget/title_bar.dart';
import 'package:flutter2/widget/top_tip.dart';
import 'package:flutter_native_contact_picker/flutter_native_contact_picker.dart';
import 'package:flutter_native_contact_picker/model/contact.dart';
import 'package:get/get.dart';

class ContactPage extends StatefulWidget {
  const ContactPage({super.key});

  @override
  State<ContactPage> createState() => _ContactPageState();
}

class _ContactPageState extends State<ContactPage> {
  PersonInfoEntity _infoEntity = PersonInfoEntity();

  @override
  void initState() {
    super.initState();
    DioManager.request<PersonInfoEntity>(
      path: HttpApi.getInfo,
      onSuccess: (data) {
        if (data == null) return;
        _infoEntity = data;
        setState(() {});
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: mainBg,
      appBar: TitleBar.buildAppBar(title: "紧急联系人"),
      body: SafeArea(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(left: 16, right: 16, top: 10),
              child: StepView(max: 7, step: 4),
            ),
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    TopTip(title: "紧急联系人"),
                    SizedBox(
                      height: 20,
                    ),
                    ...getItems(1),
                    SizedBox(
                      height: 20,
                    ),
                    ...getItems(2),
                  ],
                ),
              ),
            ),
            NormalButton(
              click: () {
                var param = getParams();
                param["worthlesselectronic"] = "1";
                param["shoechildbefore"] = _infoEntity.num1;
                param["here"] = _infoEntity.name1;
                param["firefighterpurebend"] = "2";
                param["photographerunrestopposite"] = _infoEntity.num2;
                param["themeterrify"] = _infoEntity.name2;
                DioManager.request(
                  path: HttpApi.saveContacts,
                  data: param,
                  onSuccess: (data) {
                    Get.off(AddCardPage(loan: true));
                  },
                );
              },
              enable: _infoEntity.num1.isNotEmpty &&
                  _infoEntity.name1.isNotEmpty &&
                  _infoEntity.num2.isNotEmpty &&
                  _infoEntity.name2.isNotEmpty,
              width: double.infinity,
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 20),
              text: "确定",
            ),
          ],
        ),
      ),
    );
  }

  List<Widget> getItems(int index) {
    return [
      BaseContainer(
        width: null,
        margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
        child: NormalText(
          fontSize: 16,
          text: "联系人$index",
          fontColor: mainText,
          fontWeight: FontWeight.bold,
        ),
      ),
      InfoBar(
        click: () => pick(index),
        title: "姓名",
        value: index == 1 ? _infoEntity.name1 : _infoEntity.name2,
      ),
      InfoBar(
        click: () => pick(index),
        title: "手机号",
        value: index == 1 ? _infoEntity.num1 : _infoEntity.num2,
      )
    ];
  }

  pick(int index) async {
    try {
      var picker = FlutterNativeContactPicker();
      Contact? contact = await picker.selectPhoneNumber();
      if (contact == null) return;
      if (index == 1) {
        _infoEntity.num1 = contact.selectedPhoneNumber ?? "";
        _infoEntity.name1 = contact.fullName ?? "";
      } else {
        _infoEntity.num2 = contact.selectedPhoneNumber ?? "";
        _infoEntity.name2 = contact.fullName ?? "";
      }
    } catch (e) {
    } finally {
      setState(() {});
    }
  }
}
